library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.0.4     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
LUFile<-read_csv("s84_land_cover_basin.csv")
## Rows: 23952 Columns: 17
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (6): Model, scenario, region, riverBasin, Variable, Unit
## dbl (11): 2010, 2015, 2020, 2025, 2030, 2035, 2040, 2045, 2050, 2055, 2060
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
new_Shock_LU<- LUFile  %>% 
  separate(Variable, c("Land", "LandUse", "Status")) %>%
  dplyr::select(-c("Model", "Unit"))  %>%
  filter(Land == "LandCover") %>%
  filter(LandUse!= "Total") %>%
  filter(Status != "Total"|is.na(Status)) %>%
  mutate(Status = replace_na(Status, "")) %>%
  unite(LandCover, Status, LandUse, sep="")
## Warning: Expected 3 pieces. Missing pieces filled with `NA` in 6525 rows [1, 5, 6, 7,
## 11, 12, 23, 27, 28, 29, 33, 34, 45, 46, 47, 48, 52, 53, 59, 63, ...].
wide_sum_cat_LU<- new_Shock_LU %>% 
  dplyr::select(-c("Land"))  %>%
  pivot_longer(!c(scenario, region, riverBasin, LandCover), names_to=c("year"), values_to = "area") %>%
  mutate(year = as.numeric(year)) %>% 
  pivot_wider(names_from = scenario, values_from = c(area)) %>%
  mutate(DifCornShock = CORN1BG - REF) %>%
  mutate(DifSoyShock = SOY1BG - REF)

futRefLand.plt<-ggplot(wide_sum_cat_LU %>% 
                              filter(region=="WLD")) +
  geom_line(mapping=aes(x=year, y=REF, color=LandCover), lwd=2) +
  scale_color_manual(values=c("#FED439FF", "#46732EFF", "green", "#FD8CC1FF", "#D2AF81FF", "#8A9197FF", "#F05C3BFF","#075149FF", "#370335FF"),  name="Land Use", labels=c("Cropland", "Grassland", "Managed Forest", "Managed Pasture", "Other Arable Land","Other Not Arable", "Shrubland", "Unmanaged Forest", "Unmanaged Pasture")) +
  ggtitle("Future Land Use") +
  #Add the theme elements
  theme(plot.title = element_text(hjust = 0.5)) +
  #Add the labels
  labs( x='Year', y='Area (Mha)', linetype="Shock Type") 

ggplotly(futRefLand.plt)
futShocksLand.plt<-ggplot(wide_sum_cat_LU %>% 
                              filter(region=="WLD")) +
  geom_line(mapping=aes(x=year, y=DifCornShock, color=LandCover, linetype="Corn"), lwd=2) +
  geom_line(mapping=aes(x=year, y=DifSoyShock, color=LandCover, linetype="Soy"),  lwd=2) +
  scale_color_manual(values=c("#FED439FF", "#46732EFF", "green", "#FD8CC1FF", "#D2AF81FF", "#8A9197FF", "#F05C3BFF","#075149FF", "#370335FF"),  name="Land Use", labels=c("Cropland", "Grassland", "Managed Forest", "Managed Pasture", "Other Arable Land","Other Not Arable", "Shrubland", "Unmanaged Forest", "Unmanaged Pasture")) +
  ggtitle("Future Land Emission Shocks") +
  #Add the theme elements
  #theme(plot.title = element_text(hjust = 0.5)) +
  #Add the labels
  labs( x='Year', y='Area (Mha)', linetype="Shock Type") 


ggplotly(futShocksLand.plt)